package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.GalaxyCardOrder.GALAXY_CARD_ORDER;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardOrderRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardOrderRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardOrderResponse;
import com.tuanzi.server.loan_base.util.DateUtils;

@SuppressWarnings({ "rawtypes" })
@Repository
public class GalaxyCardOrderRepositoryImpl extends JOOQBaseRepository implements GalaxyCardOrderRepository{

	@Override
	protected TableImpl getTable() {
		return GALAXY_CARD_ORDER;
	}

	@Override
	public Page<GalaxyCardOrderResponse> list(
			GalaxyCardOrderRequest searchRequest, Pageable pageable) {
		
		fields(GALAXY_CARD_ORDER.fields());
		eq(GALAXY_CARD_ORDER.PLATFORM, searchRequest.getPlatform());
		eq(GALAXY_CARD_ORDER.PRD_ID, searchRequest.getPrdId());
		eq(GALAXY_CARD_ORDER.CHANNEL, searchRequest.getChannel());
		eq(GALAXY_CARD_ORDER.PAY_TYPE, searchRequest.getPayType());
		eq(GALAXY_CARD_ORDER.USER_ID, searchRequest.getUserId());
		eq(GALAXY_CARD_ORDER.MOBNUM, searchRequest.getMobnum());
		eq(GALAXY_CARD_ORDER.ORDER_STATUS, searchRequest.getOrderStatus());
		eq(GALAXY_CARD_ORDER.ENTRANCE, searchRequest.getEntrance());
		eq(GALAXY_CARD_ORDER.ORDER_FROM,searchRequest.getOrderFrom());
		
		between(GALAXY_CARD_ORDER.CREATE_TIME, searchRequest.getStartDate(), 
				searchRequest.getEndDate() == null ? searchRequest.getEndDate() : 
					DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + " 23:59:59");
		
		orderBy(GALAXY_CARD_ORDER.CREATE_TIME, Sort.Direction.DESC);
		 return search(pageable, record -> {
			 GalaxyCardOrderResponse response = new GalaxyCardOrderResponse();
	            response.setId(record.get(GALAXY_CARD_ORDER.ID));
	            response.setCreateTime(record.get(GALAXY_CARD_ORDER.CREATE_TIME));
	            response.setPayTime(record.get(GALAXY_CARD_ORDER.PAY_TIME));
	            response.setOrderId(record.get(GALAXY_CARD_ORDER.ORDER_ID));
	            
	            String phone = record.get(GALAXY_CARD_ORDER.MOBNUM);
	            response.setMobnum(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
	            
	            response.setPlatform(record.get(GALAXY_CARD_ORDER.PLATFORM));
	            response.setPrdId(record.get(GALAXY_CARD_ORDER.PRD_ID));
	            response.setChannel(record.get(GALAXY_CARD_ORDER.CHANNEL));
	            response.setOrderPayAmount(record.get(GALAXY_CARD_ORDER.ORDER_PAY_AMOUNT).floatValue());
	            response.setPayType(record.get(GALAXY_CARD_ORDER.PAY_TYPE).intValue());
	            response.setOrderStatus(record.get(GALAXY_CARD_ORDER.ORDER_STATUS).intValue());
	            response.setEntrance(record.get(GALAXY_CARD_ORDER.ENTRANCE));
	            response.setOrderFrom(record.get(GALAXY_CARD_ORDER.ORDER_FROM));
	            return response;
	        });
	}

	
}
